package main

import (
	"context"
	"errors"
	"micro/lesson1_basic_GRPC/proto/user"
	"net"

	"google.golang.org/grpc"
)

// 服务端代码实现
type UserServer struct{}

func (*UserServer) GetUser(ctx context.Context, req *user.GetUserReq) (*user.GetUserResp, error) {
	if req.Id < 1 || req.Id > 100 {
		return nil, errors.New("用户未找到")

	}
	return &user.GetUserResp{
		Id:    "1",
		Name:  "zhangsan",
		Phone: "123456789",
	}, nil

}

func main() {
	grpcServer := grpc.NewServer(grpc.ChainUnaryInterceptor(LogInterceptor))
	user.RegisterUserServer(grpcServer, new(UserServer))
	listen, err := net.Listen("tcp", ":8080")
	if err != nil {
		panic(err)
	}
	grpcServer.Serve(listen)
}
